<template>
	<view class="card">
		<view class="ul">
			<view class="li" v-for="(item,index) in treeData" :key="index">
				<view class="item" :class="{'line-left':index!==0,'line-right':index!=treeData.length-1}">
					<view class="item-name"
						:class="{'line-bottom':item.info_list&&item.info_list.length>0&&(item.is_look==1 || !item.is_look),'line-top':!treeFirst}">
						<view @click="item.is_look==1?toSiha(item):''" style="display: flex;flex-direction: column;justify-content: center;align-items: center;">
							<image :style="{'filter':item.is_look==1?'grayscale(0)':'grayscale(100%)'}" v-if="item.thumbnail" style="width: 200rpx;height: 120rpx;border-radius: 10rpx;float: left;z-index: 101;" class="treeImage" :src="getImg(item.thumbnail)" mode="aspectFill"></image>
							<text :style="{'opacity':(item.is_look==1 || !item.is_look)?1:0.5}" style="font-size: 24rpx;text-align: center;padding-top: 20rpx;">{{item.name}}</text>
						</view>
					</view>
				</view>
				<TreeItem v-if="item.info_list&&item.info_list.length>0 && (item.is_look==1 || !item.is_look) && isShow" :ls-data="lsData" :tree-data="item.info_list" />
			</view>
		</view>
	</view>
</template>
<script>
	import TreeItem from '@/components/tree-item/tree-item.vue'
	export default {
		components: {
			TreeItem
		},
		name: 'TreeItem',
		props: {
			lsData:{
				type: Array,
				default: () => []
			},
			treeData: {
				type: Array,
				default: () => []
			},
			treeFirst: { 
				//判断是否第一个
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				isShow:false
			}
		},
		mounted() {
			this.isShow = false
			this.comNext()
		},
		methods: {
			comNext(){
				for (let s of this.treeData) {
					if(!s.info_list){
						s.info_list = []
					}
					let lsArr = (s.xia_ids.toString()).split(',')
					console.log(lsArr)
					if(lsArr.indexOf('0')>-1){
						s.info_list = []
						return
					} 
					let listArr = this.lsData.filter((value,index,arr)=>{
						return lsArr.indexOf(value.id+'')>-1
					})
					s.info_list = listArr
				}
				this.$nextTick(()=>{
					setTimeout(()=>{
						this.isShow = true
						this.treeData = [...this.treeData]
					},2000)
				})
			},
			toSiha(e){
				console.log(e)
				if(e.is_look){
					if(e.video_type==2){
						this.comLink('/pages/index/siha?id='+e.id)
					}else{
						this.comLink('/pages/index/siha2?id='+e.id)
					}
				}
			},
			// 添加子元素
			addChild(index) {
				if (this.treeData[index].info_list) {
					this.treeData[index].info_list.push({
						name: '1'
					})
				} else {
					this.$set(this.treeData[index], 'info_list', [{
						'name': '1'
					}])
				}
			},
			// 删除子元素
			delChild(index) {
				this.treeData.splice(index, 1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	$line-length: 40px; //线长
	$spacing: 40px; //间距
	$extend: calc(#{$spacing} + 2px); //延长线

	// 线样式
	@mixin line {
		content: "";
		display: block;
		width: 3px;
		height: $line-length;
		position: absolute;
		left: 0;
		right: 0;
		margin: auto;
		// background: rgb(6, 183, 253);
	}

	// 加减号icon
	@mixin icon {
		position: absolute;
		border: solid 1px #666;
		border-radius: 100%;
		bottom: -5px;
		width: 12px;
		height: 12px;
		line-height: 10px;
		text-align: center;
		color: #666;
		z-index: 100;
		cursor: pointer;
		margin: 0;
		padding: 0;
		// opacity: 0;
	}

	.card {
		z-index: 101;
		.ul {
			display: flex;
			justify-content: center;

			.li {
				.item {
					display: flex;
					justify-content: center;
					align-items: center;
					position: relative;
					&-name {
						position: relative;
						display: flex;
						justify-content: center;
						align-items: center;
						width: 100px;
						height: 50px;
						padding: 5px;
						margin: $spacing;
						// background: coral;
						color: #fff;

						.plus {
							@include icon();

							&:hover {
								opacity: 100%;
							}
						}

						.reduce {
							@include icon();
							top: -5px;

							&:hover {
								opacity: 100%;
							}
						}
					}
				}
			}

		}

		// 向下的线
		.line-bottom {
			&::after {
				@include line();
				bottom: -$line-length;
				width: 0;
				border-left: 3rpx dashed rgba(#D8D8D8,0.5);
			}
		}

		// 向上的线
		.line-top {
			&::before {
				@include line();
				top: -$line-length;
				width: 0;
				border-left: 3rpx dashed rgba(#D8D8D8,0.5);
			}
		}

		// 向左的线
		.line-left {
			&::after {
				@include line();
				width: calc(50% + #{$spacing});
				height: 0px;
				border-top: 3rpx dashed rgba(#D8D8D8,0.5);
				left: calc(-50% - #{$extend});
				top: 0;
			}
		}

		// 向右的线
		.line-right {
			&::before {
				@include line();
				width: calc(50% + #{$spacing});
				height: 0px;
				border-top: 3rpx dashed rgba(#D8D8D8,0.5);
				right: calc(-50% - #{$extend});
				top: 0;
			}
		}
	}
</style>